<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

		<link rel="stylesheet" href="style.css">
		<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>

		<title>PhoneWars</title>
	</head>
	<body>

<?php
# Logging in with Google accounts requires setting special identity
require 'openid.php';
require 'pw.php';

try {
	# Change 'localhost' to your domain name.
	$openid = new LightOpenID('pba.cs.clemson.edu');
	if(!$openid->mode) {
		if(isset($_GET['login'])) {
			$openid->identity = 'https://www.google.com/accounts/o8/id';
			header('Location: ' . $openid->authUrl());
		}
?>

		<div id="login">
			<div class="box-inside">
				<img class="logo-img" src="img/logo.png" alt="Phone Wars"/><br /><br />
				<a class="large awesome green" href="?login">Log In with Google &raquo;</a> <br /><br />
				<!-- <a class="large awesome yellow">Log In with Yahoo &raquo;</a> <br /><br /> -->
				<!-- <a class="large awesome magenta">Log In with Facebook &raquo;</a> <br /><br /> -->
				<!-- <a class="openid-btn google" title="log in with Google" href="?login"></a><br /> -->
			</div>
		</div>
<!--
	<p class="pw-title">PhoneWars</p>
	<p class="pw-subtitle">Phone-to-Phone Combat</p>
	<a class="large awesome" href="?login">Log In with Google &raquo;</a><br /><br />
*/
-->

<!--
<form action="?login" method="post">
	<button>Login with Google</button>
</form>
-->

<?php
	} elseif($openid->mode == 'cancel') {
		echo 'User has canceled authentication!';
	} else {
		session_start();
		$querystring = $_SERVER['QUERY_STRING'];

		$pw = new PhoneWars();
		$pw->begin(session_id(),FALSE);
		$pw->loginAccount(htmlspecialchars($_GET["openid_identity"]));
		$pw->end();
?>

<!-- Now using an http redirect to minimize the amount of user input needed
<a href="phonewars://pw/auth?sessionid=<?php echo "$sessionid&$querystring" ?>">Continue</a><br>
-->

<!-- For some reason, on the Galaxy Tab it will not link correctly to the PhoneWars app if a form button is used.
<form action="phonewars://pw/auth?sessionid=<?php echo "$sessionid&$querystring" ?>" method="post">
	<button>Continue</button>
</form>
-->

<?php
		// echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
		if($openid->validate())
		{
			header("Location: phonewars://pw/auth?sessionid=".session_id()."&$querystring");
		}
	}
} catch(ErrorException $e) {
	$output = array('PW_ERROR' => $e->getMessage());
	print(json_encode($output));
}
?>

	</body>
</html>
